﻿using System;

namespace Cos.Module.Template.NHibernate.Test
{
    using System.Collections.Generic;

    using Cos.Core.NHibernate;
    using Cos.Core.NHibernate.Tests;

    using NUnit.Framework;

    public class ModuleFixtureBase : FixtureBase
    {
        public TemplateDataModel Template { get; private set; }
        public DataTypeDataModel DataType { get; private set; }
        public PartDataModel Part { get; private set; }
        public CultureDataModel Culture { get; private set; }
        public OptionDataModel Option { get; private set; }

        public ModuleFixtureBase()
            : base(new TestModel())
        {
            this.Template = new TemplateDataModel
            {
                IsListView = false,
                TemplateName = "Template",
                TemplateUrl = "url"
            };

            this.DataType = new DataTypeDataModel
            {
                DataTypeName = "abcdefgh" + Guid.NewGuid(),
                HasMedia = true,
                IsFormField = true
            };

            this.Part = new PartDataModel
            {
                PartName = "part name",
                PartOrder = 1,
                IsMultiCulture = true,
                IsRequired = false
            };

            this.Culture = new CultureDataModel
            {
                Id = "en-CA",
                IsDefault = true,
            };

            this.Option = new OptionDataModel
            {
                OptionName = "OptionName",
                OptionOrder = 1
            };
        }

        [TestFixtureSetUp]
        public void ModuleSetupContext()
        {
            using (var tx = this.Session.BeginTransaction())
            {
                this.Session.SaveOrUpdate(this.Template);
                this.Session.SaveOrUpdate(this.Culture);
                this.Session.SaveOrUpdate(this.DataType);
                this.Part.TemplateId = this.Template.Id;
                this.Part.DataTypeId = this.DataType.Id;
                this.Session.SaveOrUpdate(this.Part);
                this.Option.PartId = this.Part.Id;
                this.Session.SaveOrUpdate(this.Option);

                tx.Commit();
            }

            this.Session.Flush();
            this.Session.Clear();
        }
    }
}
